﻿using B.S.BasicData.Domain.Entities;
using B.S.BasicData.Error;
using B.S.BasicData.Infrastructure.InProfaces;
using B.S.BasicData.Write.Api.Application.Command.ProductsCommand;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHander.ProductsCommandHander
{
    public class ProductsDelCommandHander : IRequestHandler<ProductsDelCommand, ApiResult<Products>>
    {
        private readonly IBaseRepository<Products> ProductsRepository;

        public ProductsDelCommandHander(IBaseRepository<Products> productsRepository)
        {
            ProductsRepository = productsRepository;
        }

        public Task<ApiResult<Products>> Handle(ProductsDelCommand request, CancellationToken cancellationToken)
        {
            ApiResult<Products> result = new ApiResult<Products>();

            var del = ProductsRepository.GetAll().FirstOrDefault(x => x.ProductsId == request.ProductsId);

            del.IsDel = true;

            var upd = ProductsRepository.Update(del).Result;

            if (upd == 1)
            {
                result.Code = ApiEnum.success;
                result.Msg = "删除成功";
            }
            else
            {
                result.Code = ApiEnum.error;
                result.Msg = "删除失败";
            }

            return Task.FromResult(result);
        }
    }
}
